package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Jingdianintroduce;
import com.victor.entity.Jingdiantupian;
import com.victor.entity.Jingquclass;
import com.victor.entity.Jingqujingdian;
import com.victor.entity.Jingqulist;
import com.victor.entity.Jingqutupian;
import com.victor.service.JingquService;
import com.victor.utils.Msg;

/**	
 * 景区管理
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/jq")
public class JingquController {

	@Autowired
	private JingquService service;

	/**
	 * 景区类别 Jingquclass
	 */
	// 获取所有景区类别
	@RequestMapping("/getClassList")
	public Msg getClassList() {
		List<Jingquclass> jingquclasses = service.getClassList();
		return Msg.success().add("jingquclasses", jingquclasses);
	}

	// 添加景区类型
	@RequestMapping("/addClass")
	public Msg addClass(Jingquclass jingquclass) {
		boolean insResult = service.addClass(jingquclass);
		if (insResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 修改类别信息
	@RequestMapping("/updateClass")
	public Msg updateClass(Jingquclass jingquclass) {
		if (jingquclass == null || jingquclass.getId() == 0) {
			return Msg.fail("参数错误");
		}
		boolean updResult = service.updateClass(jingquclass);
		if (updResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除类别
	@RequestMapping("/deleteClass")
	public Msg deleteClass(int[] cids) {
		boolean delResult = service.deleteClass(cids);
		if (delResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	/**
	 * 景区列表
	 */
	// 获取景区列表
	@RequestMapping("/getJQlist")
	public Msg getJQlist(String jqclassid, String starttime, String endtime, String searchname) {
		List<Jingqulist> jingqulists = service.getJQlist(jqclassid, starttime, endtime, searchname);

		return Msg.success().add("jingqulists", jingqulists);
	}

	// 新建景区
	@RequestMapping("/buildJQ")
	public Msg buildJQ(@RequestBody Jingqulist jingqulist) {
		// 添加景区信息
		boolean insResult = service.buildJQ(jingqulist);
		if (insResult) {
			List<Jingqutupian> jingqutupians = jingqulist.getJingqutupians();
			if (jingqutupians != null && jingqutupians.size() > 0) {
				// 添加景区图片信息
				for (Jingqutupian jingqutupian : jingqutupians) {
					// 添加景区id
					jingqutupian.setJqid(String.valueOf(jingqulist.getId()));
					service.addJingqutupian(jingqutupian);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	// 批量删除
	@RequestMapping("batchDeleteJQ")
	public Msg batchDeleteJQ(int[] jqids) {
		boolean delResult = service.batchDeleteJQ(jqids);
		return delResult ? Msg.success() : Msg.fail();
	}

	// 批量导出
	@RequestMapping("batchExportJQ")
	public void batchExportJQ(String jqclass, String starttime, String endtime, String searchname,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		// 数据库查询数据 exportlist 需要导出的list
		List<Jingqulist> jingqulists = service.getJQlist(jqclass, starttime, endtime, searchname);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("景区列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("景区列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("id");
		headRow.createCell(1).setCellValue("景区名称");
		headRow.createCell(2).setCellValue("景区价格");
		headRow.createCell(3).setCellValue("景区活动价");
		headRow.createCell(4).setCellValue("景区级别");
		headRow.createCell(5).setCellValue("景区简介");
		headRow.createCell(6).setCellValue("语音简介");
		headRow.createCell(7).setCellValue("景区类型id");
		headRow.createCell(8).setCellValue("景区类型");
		headRow.createCell(10).setCellValue("景区备注");
		headRow.createCell(11).setCellValue("创建时间");
		headRow.createCell(12).setCellValue("账号");
		headRow.createCell(13).setCellValue("密码");
		headRow.createCell(14).setCellValue("景区视频");
		headRow.createCell(15).setCellValue("活动标识");

		// 遍历上面数据库查到的数据
		for (Jingqulist jq : jingqulists) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(jq.getId());
			dataRow.createCell(1).setCellValue(jq.getJqname());
			dataRow.createCell(2).setCellValue(jq.getJqprice());
			dataRow.createCell(3).setCellValue(jq.getJqhdprice());
			dataRow.createCell(4).setCellValue(jq.getJqlevel());
			dataRow.createCell(5).setCellValue(jq.getJqjianjie());
			dataRow.createCell(6).setCellValue(jq.getJqvoice());
			dataRow.createCell(7).setCellValue(jq.getJqclassid());
			dataRow.createCell(8).setCellValue(jq.getJqclass());
			dataRow.createCell(10).setCellValue(jq.getJqbeizhu());
			dataRow.createCell(11).setCellValue(jq.getJqtime());
			dataRow.createCell(12).setCellValue(jq.getUsername());
			dataRow.createCell(13).setCellValue(jq.getPassword());
			dataRow.createCell(14).setCellValue(jq.getJqvideo());
			dataRow.createCell(15).setCellValue(jq.getHdflag());

		}
		// 下载导出文件名
		String filename = "景区列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

	}

	// 景区详情 
	@RequestMapping("/getJQxqByid")
	public Msg getJQxqByid(String jqid) {
		Jingqulist jingqulist = service.getJQxqByid(jqid);
		if (jingqulist != null) {
			return Msg.success().add("jingqu", jingqulist);
		}
		return Msg.fail("未查询到该景区");
	}

	// 修改景区信息
	@RequestMapping("/updateJQ")
	public Msg updateJQ(Jingqulist jingqulist) {
		boolean updResult = service.updateJQ(jingqulist);
		if (updResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加小景点
	@RequestMapping("/addJingDian")
	public Msg addJingDian(@RequestBody Jingqujingdian jingqujingdian) {
		// 添加景点
		boolean insResult = service.addJingdian(jingqujingdian);
		if (insResult) {
			List<Jingdiantupian> jingdiantupians = jingqujingdian.getJingdiantupians();
			if (jingdiantupians != null && jingdiantupians.size() > 0) {
				for (Jingdiantupian jingdiantupian : jingdiantupians) {
					jingdiantupian.setJdid(String.valueOf(jingqujingdian.getId()));
					// 添加景点图片
					service.addJingdiantupian(jingdiantupian);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除景点
	@RequestMapping("/deleteJingdianByid")
	public Msg deleteJingdianByid(String jdid) {
		boolean delResult = service.deleteJingdianByid(jdid);
		if (delResult) {
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加景点简介
	@RequestMapping("/addJdintroduce")
	public Msg addJdintroduce(Jingdianintroduce jingdianintroduce) {
		boolean insResult = service.addJdintroduce(jingdianintroduce);
		return Msg.judge(insResult);
	}

	// 修改景点
	@RequestMapping("/updateJD")
	public Msg updateJD(@RequestBody Jingqujingdian jingqujingdian) {
		boolean updResult = service.updateJD(jingqujingdian);
		if (updResult) {
			// 修改景点图片list
			List<Jingdiantupian> jingdiantupians = jingqujingdian.getJingdiantupians();
			if (jingdiantupians != null && jingdiantupians.size() > 0) {
				// 删除景点原图片
				service.deleteJingdianTup(jingqujingdian.getId());
				for (Jingdiantupian jingdiantupian : jingdiantupians) {
					service.addJingdiantupian(jingdiantupian);
				}
			}
			// 修改景点简介list
			List<Jingdianintroduce> jingdianintroduces = jingqujingdian.getJingdianintroduces();

			if (jingdiantupians != null && jingdiantupians.size() > 0) {
				// 删除景点原简介
				service.deleteJingdianJJ(jingqujingdian.getId());
				for (Jingdianintroduce jingdianintroduce : jingdianintroduces) {
					service.addJdintroduce(jingdianintroduce);
				}
			}
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除评论
	@RequestMapping("/deleteCommentByid")
	public Msg deleteCommentByid(String cid) {
		boolean delResult = service.deleteCommentByid(cid);
		return Msg.judge(delResult);
	}
}
